package com.cskaoyan.controller.stage;

import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.StorageVO;
import com.cskaoyan.service.StorageService;
import com.cskaoyan.util.FileProcessUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @ClassName: WxStorageController
 * @Description: TODO 图片上传模块
 * @Author:zhoukang
 * @Date: 2022/5/7 17:18
 * @Version: 1.0
 **/
@RestController
@RequestMapping("wx/storage")
public class WxStorageController {
    @Autowired
    StorageService storageService;

    /**
     * @author: zhoukang
     * @description: TODO 图片上传
     * @date: 2022/5/7 17:41
     */
    @PostMapping("upload")
    public BaseRespVO<StorageVO> upload(MultipartFile file){
        //获取接收文件的信息
        StorageVO fileInfo = FileProcessUtil.getFileInfo(file);
        fileInfo.setKey(fileInfo.getKey().substring(37));
        fileInfo.setUrl("http://localhost:8083/image/"+fileInfo.getKey());
        try {
            file.transferTo(new File("D:\\JavaWorkspace\\tmp", fileInfo.getKey()));
        }catch (IOException e) {
            e.printStackTrace();
        }
        fileInfo.setAddTime(new Date());
        fileInfo.setUpdateTime(new Date());
        storageService.create(fileInfo);
        return BaseRespVO.ok(fileInfo);
    }

}
